#!/usr/bin/env pytho      
# -*- coding: utf-8 -*-
# @Author  : junpeng_chen
# @Time    : 2023/7/27 16:24
# @File    : measure_recognition_test.py
# @annotation    : 测试

import unittest
from utils.chatglm_utils import ChatGLMUtils
from modules.measure_recognition import MeasureRecognition

measure_reg = MeasureRecognition(None)


class TestMeasureRecognition(unittest.TestCase):

    def test_filter(self):
        """
        测试filter函数
        :return:
        """
        query = "今年合同金额大于五百万的分部有哪些？"
        literals = [{'member_amount': 0, 'name': '年', 'literal': '2023', 'operator': '==', 'timeLevel': 'year',
                     'id': 'AUGMENTED_DATASET_LEVEL.I8a8ae5ca0178549554951b9501785cefe3f00058.MT_DIMENSION_FIELD.02de0a83c07e6c06b5f6dc8d09f9fce7-C_DATE_Year-LEVEL-1648634671682',
                     'type': 'STRING'},
                    {'member_amount': 23, 'name': '销售分部', 'literal': 'none', 'operator': 'none', 'timeLevel': '',
                     'id': 'AUGMENTED_DATASET_FIELD.I8a8ae5ca0178549554951b9501785cefe3f00058.MT_DIMENSION_FIELD.2b4ff0f606441075f12c1be466e3642e',
                     'type': 'STRING'},
                    {'member_amount': 0, 'name': '合同金额', 'literal': '大于五百万', 'operator': '>', 'timeLevel': '',
                     'id': 'AUGMENTED_DATASET_MEASURE.I8a8ae5ca0178549554951b9501785cefe3f00058.MT_METRICS.I8a8ae5ca0178549554951b9501785d983aaa005e',
                     'type': 'DOUBLE'}]
        res = measure_reg.filter(query, literals)
        ans = [{'member_amount': 0, 'name': '合同金额', 'literal': '大于五百万', 'operator': '>', 'timeLevel': '',
                'id': 'AUGMENTED_DATASET_MEASURE.I8a8ae5ca0178549554951b9501785cefe3f00058.MT_METRICS.I8a8ae5ca0178549554951b9501785d983aaa005e',
                'type': 'DOUBLE'}]
        self.assertEqual(res, ans)

    def test_is_charset_legal(self):
        test_string1 = "123,456(789)[0,1,2,3]"
        test_string2 = "hello,world!"
        test_string3 = "123,456{789}[0,1,2,3]"
        test_string4 = "none"
        test_string5 = "123abc"
        test_string6 = "12"
        test_string7 = "[12,13]"
        test_string8 = "(12,none}"
        test_string9 = "贾大娘(12,none}"
        self.assertEqual(measure_reg.is_charset_legal(test_string1), True)
        self.assertEqual(measure_reg.is_charset_legal(test_string2), False)
        self.assertEqual(measure_reg.is_charset_legal(test_string3), True)
        self.assertEqual(measure_reg.is_charset_legal(test_string4), True)
        self.assertEqual(measure_reg.is_charset_legal(test_string5), False)
        self.assertEqual(measure_reg.is_charset_legal(test_string6), True)
        self.assertEqual(measure_reg.is_charset_legal(test_string7), True)
        self.assertEqual(measure_reg.is_charset_legal(test_string8), True)
        self.assertEqual(measure_reg.is_charset_legal(test_string9), False)

    def test_has_only_one_pair_of_brackets(self):
        test_string1 = "(Hello)"
        test_string2 = "No parentheses here!"
        test_string3 = "(Only one parenthesis"
        test_string4 = "Another ) parenthesis"
        test_string5 = "{1,2]"
        test_string6 = ")1,2["

        self.assertEqual(measure_reg.has_only_one_pair_of_brackets(test_string1), 1)
        self.assertEqual(measure_reg.has_only_one_pair_of_brackets(test_string2), 0)
        self.assertEqual(measure_reg.has_only_one_pair_of_brackets(test_string3), -1)
        self.assertEqual(measure_reg.has_only_one_pair_of_brackets(test_string4), -1)
        self.assertEqual(measure_reg.has_only_one_pair_of_brackets(test_string5), 1)
        self.assertEqual(measure_reg.has_only_one_pair_of_brackets(test_string6), -1)

    def test_formatter(self):
        test_string1 = "(4, 8}"
        test_string2 = "non[4,5]9"
        test_string3 = "【12,none)"
        test_string4 = "27"
        test_string5 = "{4,none]"
        test_string6 = "{none,9]"
        test_string7 = "【1，5,9]"
        test_string8 = "{}"



        self.assertEqual(measure_reg.formatter(test_string1), [('>', '4'), ('<=', '8')])
        self.assertEqual(measure_reg.formatter(test_string2), [('in', '[4,5]')])
        self.assertEqual(measure_reg.formatter(test_string3), [('>', '12')])
        self.assertEqual(measure_reg.formatter(test_string4), [('==', '27')])
        self.assertEqual(measure_reg.formatter(test_string5), [('>=', '4')])
        self.assertEqual(measure_reg.formatter(test_string6), [('<', '9')])
        self.assertEqual(measure_reg.formatter(test_string7), [('in', '[1,5,9]')])
        self.assertEqual(measure_reg.formatter(test_string8), [])

